import { mergeOptions } from "../util/index";

export function initGlobalAPI(Vue) {
    Vue.options = {};
    Vue.mixin = function (mixin) {
        //实现两个对象的合并
        this.options = mergeOptions(this.options, mixin);
    };

    Vue.mixin({
        a: 1,
        beforeCreate() {
            console.log("mixin1");
        },
    });

    Vue.mixin({
        b: 2,
        beforeCreate() {
            console.log("mixin2");
        },
    });
    console.log(Vue.options, "options777");

    Vue.use = function (plugin) {
        const installedPlugins =
            this._installedPlugins || (this._installedPlugins = []);
        if (installedPlugins.indexOf(plugin) > -1) {
            //判断插件是否被注册过
            return this;
        }

        // 其他参数
        const args = toArray(arguments, 1);
        args.unshift(this);
        //对plugin参数类型区分，支持函数类型和对象类型
        if (typeof plugin.install === "function") {
            plugin.install.apply(plugin, args);
        } else if (typeof plugin === "function") {
            plugin.apply(null, args);
        }
        installedPlugins.push(plugin);
        return this;
    };
}
